//#include "../include/MainFrame.h"
#include "../include/AccountManager.h"
#include "../include/Account.h"

/**************************************************************************
     CTransactionRegisterTable
**************************************************************************/

CTransactionRegisterTable::CTransactionRegisterTable(CAccount* pParent, wxWindowID ID, CAccountPlugin* pPlugin, CMySqlWrapper* pMySql)
: wxGrid(pParent, ID),
  m_pParent(pParent), m_pPlugin(pPlugin), m_pMySql(pMySql)
{
    // Construct a wxGridTableBase object and attach it to the grid
    wxFont LabelFont(11, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
    CTransactionRegisterData* pTableData = new CTransactionRegisterData(this);
    SetTable(pTableData, true);
    SetLabelFont(LabelFont);
    SetColLabelAlignment(wxALIGN_LEFT, wxALIGN_CENTRE);
    SetRowLabelSize(25);
    for (int x = 0; x < GetNumberCols(); x++)
    {
        SetColSize(x, -1);
    }
}

CTransactionRegisterTable::~CTransactionRegisterTable()
{

}

CAccount* CTransactionRegisterTable::GetAccount() const
{
    return m_pParent;
}

CMySqlWrapper* CTransactionRegisterTable::GetMySql() const
{
    return m_pMySql;
}

CAccountPlugin* CTransactionRegisterTable::GetPlugin() const
{
    return m_pPlugin;
}

/**************************************************************************
     CTransactionRegisterData
**************************************************************************/

CTransactionRegisterData::CTransactionRegisterData(CTransactionRegisterTable* pParent)
: m_pParent(pParent), m_NumRows(0)
{
    // set the column headers
    m_pParent->GetPlugin()->GetColumnInfo(m_ColHeaderList);

    // set the number of rows
    wxString Query;
    Query = wxString::Format("SELECT * FROM %s.%s",
                             m_pParent->GetMySql()->GetDefaultDatabase(),
                             m_pParent->GetAccount()->GetAccountInfo()->AccountFileName);
    if (m_pParent->GetMySql()->RunQuery(Query) )
    {
        MYSQL_ROW row;
        while (row = m_pParent->GetMySql()->GetNextRowFromResultSet() )
        {
            m_NumRows++;
        }
    }
}

CTransactionRegisterData::~CTransactionRegisterData()
{

}

//(*Override from wxGridTableBase
int CTransactionRegisterData::GetNumberRows()
{
    return m_NumRows;
}

int CTransactionRegisterData::GetNumberCols()
{
    return m_ColHeaderList.size();
}

wxString CTransactionRegisterData::GetValue(int Row, int Col)
{

}

void CTransactionRegisterData::SetValue(int Row, int Col, const wxString& Value)
{
    return;
}

bool CTransactionRegisterData::AppendCols(size_t NumCols)
{
    return true;
}

bool CTransactionRegisterData::InsertCols(size_t Pos, size_t NumCols)
{
    return true;
}

wxString CTransactionRegisterData::GetColLabelValue(int Col)
{
    return m_ColHeaderList[Col].Header;
}

void CTransactionRegisterData::SetColLabelValue(int Col, const wxString& Label)
{
    return;
}

//*)

/**************************************************************************
     CAccount
**************************************************************************/

CAccount::CAccount(CAccountManager* pParent, wxWindowID ID, SAccountInfo& AccountInfo, CAccountPlugin* pPlugin, CMySqlWrapper* pMySql)
: wxPanel(pParent, ID),
  m_pParent(pParent), m_AccountInfo(AccountInfo), m_pPlugin(pPlugin), m_pMySql(pMySql)
{
    // create the toolbar
    int idToolBegin = wxNewId();
    int idToolBack = wxNewId();
    int idToolForward = wxNewId();
    int idToolEnd = wxNewId();
    int idToolAccountDetails = wxNewId();
    int idToolBalanceAccount = wxNewId();
    int idToolNewTransaction = wxNewId();
    int idToolEditTransaction = wxNewId();
    int idToolDeleteTransaction = wxNewId();
    int idToolPrintTransactions = wxNewId();
    int idToolSearchTransactions = wxNewId();
    int idToolReconciledCheckBox = wxNewId();

    m_pToolbar = new wxToolBar(this, wxID_ANY);
    m_pToolbar->SetMargins(5, 5);
    m_pToolbar->SetToolSeparation(15);
    m_pToolbar->SetToolBitmapSize(wxSize(18, 18) );
    m_pToolbar->AddTool(idToolBegin, wxEmptyString, wxICON(mmIconDoubleBack), _("Go to Begining") );
    m_pToolbar->AddTool(idToolBack, wxEmptyString, wxICON(mmIconBack), _("Go back") );
    m_pToolbar->AddTool(idToolForward, wxEmptyString, wxICON(mmIconForward), _("Go forward") );
    m_pToolbar->AddTool(idToolEnd, wxEmptyString, wxICON(mmIconDoubleForward), _("Go to end") );
    m_pToolbar->AddSeparator();
    m_pToolbar->AddTool(idToolNewTransaction, wxEmptyString, wxICON(mmIconNewTrans), _("Go to Begining") );
    m_pToolbar->AddTool(idToolEditTransaction, wxEmptyString, wxICON(mmIconEditTrans), _("Go to Begining") );
    m_pToolbar->AddTool(idToolDeleteTransaction, wxEmptyString, wxICON(mmIconDeleteTrans), _("Go to Begining") );
    m_pToolbar->AddTool(idToolPrintTransactions, wxEmptyString, wxICON(mmIconPrintTrans), _("Go to Begining") );
    m_pToolbar->AddTool(idToolSearchTransactions, wxEmptyString, wxICON(mmIconSearchTrans), _("Go to Begining") );
    m_pToolbar->AddSeparator();
    m_pToolbar->AddTool(idToolAccountDetails, wxEmptyString, wxICON(mmIconViewAccount), _("Go to Begining") );
    m_pToolbar->AddTool(idToolBalanceAccount, wxEmptyString, wxICON(mmIconBalanceAccount), _("Balance this Account") );
    m_pToolbar->AddStretchableSpace();
    m_pToolbar->AddControl(m_pHideReconciled = new wxCheckBox(this, idToolReconciledCheckBox, _("Hide Reconciled") ) );
    m_pToolbar->Realize();

    // create thetransaction register
    m_pTransactionRegister = new CTransactionRegisterTable(this, wxID_ANY, m_pPlugin, m_pMySql);

    // add controls to the panel
    wxBoxSizer* pSizer = new wxBoxSizer(wxVERTICAL);
    pSizer->Add(m_pToolbar, wxSizerFlags(0).Expand().Border(wxALL, 5) );
    pSizer->Add(m_pTransactionRegister, wxSizerFlags(0).Expand().Border(wxALL, 5) );
    SetSizerAndFit(pSizer);

    // attach handlers

}

CAccount::~CAccount()
{

}

const SAccountInfo* CAccount::GetAccountInfo() const
{
    return &m_AccountInfo;
}

bool CAccount::SaveAccountData(const bool IsNewAccount)
{

}

bool CAccount::CreateTransactionTable()
{

}

bool CAccount::LoadTransactionTable()
{

}

bool CAccount::SaveTransaction(const STransaction& Transaction, const bool IsNewTransction)
{

}

bool CAccount::ViewTransaction(STransaction& Transaction, const bool IsNew, const bool CanEdit)
{

}

void CAccount::SetTotalBalance(const double Balance)
{

}

bool CAccount::CalculateTotalBalance()
{

}



